package com.rms.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.rms.domain.Hr;
import com.rms.domain.ResumeJob;
import com.rms.utils.servlet.BaseServlet;

/**HR的各种操作
 * @author Zheng weiyu
 * @date 2019年7月30日
 */
@WebServlet("/HRServlet")
public class HRServlet extends BaseServlet {

	private final static String SESSION_NAME="hr";
	
	
	/**封装表单数据
	 * @param req
	 * @return
	 * @author Zheng weiyu
	 * 2019年7月30日
	 */
	private Hr toBean(HttpServletRequest req) {
		Hr hr = new Hr();
		try {
			BeanUtils.populate(hr, req.getParameterMap());
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hr;
	}
//在BaseServlet中实现
//	public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		//封装表单信息
//		Hr hr = toBean(req);
//		//调用service的login方法登录
//		try {
//			Hr w = hrService.login(hr,Hr.class);
//			req.getSession().setAttribute(SESSION_NAME, w);
//			//成功转发
//			req.getRequestDispatcher("main.jsp").forward(req, resp);
//		} catch (Exception e) {
//			//失败转发
//			e.printStackTrace();
//			req.setAttribute("message", "登录失败" + e.getMessage());
//			req.getRequestDispatcher("login.jsp").forward(req, resp);
//		}
//	}

	

	public void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 退出
		 */
		try {
			req.getSession().removeAttribute(SESSION_NAME);
			req.setAttribute("message", "退出成功");
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void updateResumeState(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 修改简历状态
		 * 
		 * */
		//得到resumeJob_id
		int index =Integer.parseInt(req.getParameter("resumeJob_id"));//"resumeJob_id"
		//得到准备更改的状态
		int state = Integer.parseInt(req.getParameter("resume_job_state"));
		List<ResumeJob> list = ((Hr)req.getSession().getAttribute(SESSION_NAME)).getResumeJobs();
		ResumeJob resumeJob = null;
		for(ResumeJob r:list) {
			if(r.getId()==index) {
				resumeJob = r;
				break;
			}
		}
		//得到索引指向的resumeJo״̬
		resumeJob.setState(state);
		//调用service的方法变更简历状态
		try {
			hrService.updateResumeState(resumeJob);
			req.setAttribute("message", "变更成功");
		}catch(Exception e) {
			e.printStackTrace();
			req.setAttribute("message", "变更失败");
		}
		req.getRequestDispatcher("ToHRMainPageServlet").forward(req, resp);
	
	} 
}
